# 定义一个节点类
class node:
    def __init__(self,NetName,NextJump,JumpStep):
        self.NetName=NetName
        self.NextJump=NextJump
        self.JumpStep=JumpStep
    
    def __lt__(self,n):#重载<符号
        a=self.NetName[3:]
        b=n.NetName[3:]
        return a<b

#定义一个表类
class table:
    def __init__(self):
        self.nodes=[]
    
    #判断一个路由器在原先的路由表中是否存在
    def IsExist(self,NetName):
        flag=False
        for node in self.nodes:
            if node.NetName==NetName:
                flag=True
                break
        
        return flag

    #向路由表中尝试添加一个表项，当然，这个表项不满足添加条件时会被丢弃
    def add_node(self,NetName,NextJump,JumpStep):
        NewNode=node(NetName,NextJump,JumpStep)
        if self.IsExist(NetName):
            Len=len(self.nodes)
            for i in range(Len):
                if self.nodes[i].NetName==NetName:
                    if self.nodes[i].NextJump==NextJump:
                        self.nodes[i].JumpStep=JumpStep
                    else:
                        if self.nodes[i].JumpStep>JumpStep:
                            self.nodes[i].NextJump=NextJump
                            self.nodes[i].JumpStep=JumpStep
        else:
            self.nodes.append(NewNode)
    
    #从一个list中读取信息添加进这个路由表
    def add_RouterTable(self,RouterTable):
        for t in RouterTable:
            self.add_node(t[0],t[2],t[1])

    #为了输出美观，会将路由表按照路由器名称排序 
    def sort(self):
        self.nodes.sort()
    
    #输出路由表中的信息
    def print(self):
        self.sort()
        for n in self.nodes:
            print('NetName:%s,JumpStep:%d,NextJump:%s'%(n.NetName,n.JumpStep,n.NextJump))
        
            
RouterTable1=[
    ['Net1',7,'A'],
    ['Net2',2,'C'],
    ['Net6',8,'F'],
    ['Net8',4,'E'],
    ['Net9',4,'F']
]
RouterTable2=[
    ['Net2',5,'C'],
    ['Net3',9,'C'],
    ['Net6',5,'C'],
    ['Net8',4,'C'],
    ['Net9',6,'C']
]

old_table=table()
old_table.add_RouterTable(RouterTable1)

add_table=table()
add_table.add_RouterTable(RouterTable2)

print('旧路由表')
old_table.print()

print('添加的路由表')
add_table.print()

print('新路由表')

for n in add_table.nodes:
    old_table.add_node(n.NetName,n.NextJump,n.JumpStep)

old_table.print()